package com.wuxinwudai.android.content;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

/**
 * NetworkStateChangedReceiver 类为网络状态改变广播接收器类
 * @author 吾心无待
 */
public class NetworkStateChangedReceiver extends BroadcastReceiver {
    protected Context mContext;
    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo  mobNetInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiNetInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        //NetworkInfo activeInfo = connectivityManager.getActiveNetworkInfo();
        if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
            networkDisconnect();//改变背景或者 处理网络的全局变量
        }
        else if(wifiNetInfo.isConnected()) {
            wifiConnect();//WIFI连接上
        }
        else if(mobNetInfo.isConnected()) {//
            mobileConnect();//移动网络连接上
        }
//        else {
//            //改变背景或者 处理网络的全局变量
//        }
    }

    /**
     * 网络断开
     */
    public void networkDisconnect(){
        Toast.makeText(mContext, "网络已断开",Toast.LENGTH_SHORT).show();
    }

    /**
     * WIFI 连接上
     */
    public void wifiConnect(){
        Toast.makeText(mContext, "您已连接 wifi，可以放心使用啦",Toast.LENGTH_SHORT).show();
    }

    /**
     * 移动网络连接上
     */
    public void mobileConnect(){
        Toast.makeText(mContext, "您已连接移动数据\n可能会耗费大量流量哦", Toast.LENGTH_SHORT).show();
    }
}
